package com.travelms.controller;

import com.travelms.dao.DBFind;
import com.travelms.util.DataEffect;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;

public class Register {
    public Register() throws IOException, SQLException {

        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("输入exit退出输入!!!");
            System.out.println("请输入注册账号：");
            String user = sc.nextLine();
            if("exit".equals(user)){
                break;
            }
            if(!DataEffect.checkUserRule(user)){
                System.out.println("账号不符合要求，要求6-11位，只能是字母");
                continue;
            }
            System.out.println("请输入密码：");
            String password = sc.nextLine();
            if("exit".equals(password)){
                break;
            }
            if (!DataEffect.checkPasswordRule(password)){
                System.out.println("账号不符合要求，要求6-12由大小写字母和数字组成");
                continue;
            }
            System.out.println("请输入找回凭证，请认真输入妥善保管");
            String key = sc.nextLine();
            DBFind dbFind = new DBFind();
            if(dbFind.registerUser(user,password,key)){
                System.out.println("注册成功");
                try {
                    System.out.println("3秒后自动退出");
                    System.out.println("3");
                    Thread.sleep(1000);
                    System.out.println("2");
                    Thread.sleep(1000);
                    System.out.println("1");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                break;
            }else {
                System.out.println("注册失败,该用户已存在");
            }
            dbFind.connection.close();
        }


    }
}
